using DAL = IrisGenerator.Common.DatabaseModel.DataAccess;
using IrisGenerator.Common.Tools;

namespace IrisGenerator.Common.DatabaseModel {
    
    
    public partial class Column {
        public override bool Equals(object obj)
        {
            Column extObj = obj as Column;
            if (extObj == null)
                return false;

            if (this.ColId.HasValue && extObj.ColId.HasValue &&
                this.ColId.Equals(extObj.ColId)
                )
                return true;

            if (extObj.Table == null || extObj.Table.Database==null)
                return false;

            if (this.Table == null || this.Table.Database==null)
                return false;

            //Check if Databases are equals
            if (this.Table.Database.DbId.HasValue && extObj.Table.Database.DbId.HasValue)
            {
                if (!this.Table.Database.DbId.Equals(extObj.Table.Database.DbId))
                    return false;
            }
            else
            {
                if (string.IsNullOrEmpty(this.Table.Database.DbName) ||
                    string.IsNullOrEmpty(extObj.Table.Database.DbName))
                    return false;

                if (!this.Table.Database.DbName.Equals(extObj.Table.Database.DbName))
                    return false;
            }

            //Check if Tables are equals
            if (this.Table.TabId.HasValue && extObj.Table.TabId.HasValue)
            {
                if (!this.Table.TabId.Equals(extObj.Table.TabId))
                    return false;
            }
            else
            {
                if (string.IsNullOrEmpty(this.Table.TabName) ||
                    string.IsNullOrEmpty(extObj.Table.TabName))
                    return false;

                if (!this.Table.TabName.Equals(extObj.Table.TabName) ||
                    !this.Table.TabOwner.Equals(extObj.Table.TabOwner))
                    return false;
            }

            if (!this.ColName.Equals(extObj.ColName))
                return false;

            return true;
        }

        public static Column LoadFromName(string name)
        {
            DAL.Column dbo = null;
            try
            {
                dbo = new DAL.Column();
                System.Data.DataSet ds = dbo.COLUMN_Select_FromName(name);
                Column obj = null;
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        obj = new Column();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }

        }

    }
}
